# Build a list of files in XML format for WiX

Usage() if ($#ARGV != 4);

my $prefix = $ARGV[0];
my $basedir = $ARGV[1];
my $subdir = $ARGV[2];
my $pattern = "^" . $ARGV[3] . "\$";
my $outfile = $ARGV[4];
my $seq = 1;

opendir(DIR, "$basedir\\$subdir") || die "Failed to open '$basedir\\$subdir'\n";
open(OUT, ">$outfile") || die "Could not write to $outfile.\n";
print OUT "<Include>\n";
foreach my $entry (readdir(DIR)) {
  next unless (-f "$basedir\\$subdir\\$entry");
  next unless ($entry =~ $pattern);

  my $id = $entry;
  $id =~ s/-/_/g;
  $id =~ s/\(/_/g;
  $id =~ s/\)/_/g;
  
  # Get a shortname for all files. Some of the sets we have are pretty
  # large and WiX can generate conflicting names
  my $shortname = Win32::GetShortPathName("$basedir\\$subdir\\$entry");
  $shortname =~ /\\([^\\]*)$/;
  $shortname = $1;
  $shortstr = ($shortname eq $entry)?"":" ShortName=\"F$seq\"";
  
  print OUT "<File Id=\"$prefix\_$id\" DiskId=\"1\" Name=\"$entry\"";
  print OUT "$shortstr Source=\"\$(var.PKGDIR)/$subdir/$entry\" />\n";
  
  $seq++;
}
print OUT "</Include>\n";
close(OUT);
closedir(DIR);

sub Usage(){
  print "Usage:\n";
  print "  buildflist.pl <prefix> <basedir> <subdir> <repattern> <outfile>\n\n";
  exit(1);
}
